/**
 * <p>Title: ServiceZMCP0A.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2011-10-24 下午03:31:31  张志芳  Create
 *</pre>
 */
package com.baosight.sczx.zm.cp.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cp.ProducePlan;
import com.baosight.sczx.common.zm.cInterface.cp.ProfessionalPlan;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanZC;

/**
 * 打钻计划明细
 * <p>
 * Title: ServiceZMCP15.java
 * </p>
 */
public class ServiceZMCP15 extends ServiceEPBase {
	/**
	 * 初始化
	 * <p>
	 * Function: initLoad
	 * </p>
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的
		inInfo.addBlock(EiConstant.resultBlock).set(EiConstant.limitStr, "100");//设置每页默认显示记录数
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	/**
	 * 查询
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {

		String fdProfessionalplanId = inInfo.getString("fdProfessionalplanId");
		String fdProduceplanId = inInfo.getString("fdProduceplanId");
		
		if (fdProfessionalplanId != null && !fdProfessionalplanId.trim().equals("")) {
			StaticTools.setQueryParameters(inInfo, "fdProfessionalplanId", fdProfessionalplanId, true);// 添加查询条件
			StaticTools.setQueryParameters(inInfo, "isProfessionalplan", "任意数值", true);
		}
		
		if (fdProduceplanId != null && !fdProduceplanId.trim().equals(""))
			StaticTools.setQueryParameters(inInfo, "fdProduceplanId", fdProduceplanId, true);// 添加查询条件

		EiInfo outInfo = inInfo;
		if (fdProfessionalplanId != null || fdProduceplanId != null)
			outInfo = super.query(inInfo, "ZMCPProducePlanZC.query", new ZMCPProducePlanZC());
		else
			outInfo.addBlock("result").setBlockMeta(new ZMCPProducePlanZC().eiMetadata);

		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", true, UserSession.getInSessionProperty("miningCode").toString()));
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.holeType"));
		return outInfo;
	}

	/**
	 * 新增
	 * <p>
	 * Function: insert
	 * </p>
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		// 校验专业计划或生产计划是否已上报,tempInfo.getStatus()为2表示计划已上报
		inInfo = isNotReport(inInfo);
		if (inInfo.getStatus() == 2) {
			inInfo = this.query(inInfo);
			return inInfo;
		}
		EiInfo outinfo = new EiInfo();
		ZMCPProducePlanZC _dom = new ZMCPProducePlanZC();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN_ZC"));
			_dom.setFdProfessionalplanId(inInfo.getString("fdProfessionalplanId"));
			_dom.setFdProduceplanId(inInfo.getString("fdProduceplanId"));
			_dom.setFdPlanYear(inInfo.getString("fdPlanYear"));
			_dom.setFdPlanMonth(inInfo.getString("fdPlanMonth"));
			_dom.setFdDepartment(Department.getInstance().getDepartmentNameById(_dom.getFdDepartmentId()));
			_dom.setFdCreateDepartmentId(UserSession.getInSessionProperty("orgCode").toString());
			_dom.setFdCreateDepartment(UserSession.getInSessionProperty("orgName").toString());
			_dom.setFdCreatorId(UserSession.getUserId().toString());
			_dom.setFdCreator(UserSession.getInSessionProperty("UserCname").toString());// 中文名
			_dom.setFdCreattime(StaticTools.getDateTime());
			this.dao.insert("ZMCPProducePlanZC.insert", _dom);
		}
		outinfo = this.query(inInfo);
		outinfo.setStatus(1);
		outinfo.setMsg("操作" + rowcount + "记录成功");
		return outinfo;
	}

	/**
	 * 修改
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		// 校验专业计划或生产计划是否已上报,tempInfo.getStatus()为2表示计划已上报
		inInfo = isNotReport(inInfo);
		if (inInfo.getStatus() == 2) {
			inInfo = this.query(inInfo);
			return inInfo;
		}
		EiInfo outInfo = new EiInfo();
		ZMCPProducePlanZC _dom = new ZMCPProducePlanZC();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdDepartment(Department.getInstance().getDepartmentNameById(_dom.getFdDepartmentId()));
			_dom.setFdCreattime(StaticTools.getDateTime());
			this.dao.update("ZMCPProducePlanZC.update", _dom);
		}
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}

	/**
	 * 删除
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		// 校验专业计划或生产计划是否已上报,tempInfo.getStatus()为2表示计划已上报
		inInfo = isNotReport(inInfo);
		if (inInfo.getStatus() == 2) {
			inInfo = this.query(inInfo);
			return inInfo;
		}
		EiInfo outInfo = new EiInfo();
		ZMCPProducePlanZC _dom = new ZMCPProducePlanZC();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();

		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			this.dao.delete("ZMCPProducePlanZC.delete", _dom);
		}
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}

	/**
	 * 退回数据
	 * @param inInfo
	 * @return
	 */
	public EiInfo revert(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		String fdProduceplanId = inInfo.getString("fdProduceplanId");
		String message = "";
		
		//1.0判断生产计划是否生效
		if(fdProduceplanId != null && !fdProduceplanId.equals("")) {
			Map<String, String> _map = new HashMap<String, String>();
			_map.put("fdKeyId", fdProduceplanId);
			_map.put("fdPlanStatus", "草拟");
			boolean check = ProducePlan.validate(_map);
			
			if(!check) {
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("所选择记录对应的月度计划不是草拟状态，不能退回！");
				return out;  
			}
		}
		else {
			out = this.query(inInfo);
			out.setStatus(1);
			return out;  
		}
		
		ZMCPProducePlanZC _dom = new ZMCPProducePlanZC();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		
		List<String> revertList = new ArrayList<String>();
		
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			String fdProfessionalplanId = _dom.getFdProfessionalplanId();
			
			//2.0不是上报的，是自己新增的
			if(fdProfessionalplanId == null || fdProfessionalplanId.trim().equals("")) {
				message = "所选数据中有不是专业计划上报的，不能退回";
				continue;
			}
			if(!revertList.contains(fdProfessionalplanId)) {
				revertList.add(fdProfessionalplanId);
			}
		}
		
		//2.0删除生产计划数据，根据上报部门ID
		for(int i = 0; i < revertList.size(); i++) {
			String fdProfessionalplanId = revertList.get(i);
			Map<String, String> _map = new HashMap<String, String>();			
			_map.put("fdProfessionalplanId", fdProfessionalplanId);
			
			int result_zc = this.dao.delete("ZMCPProducePlanZC.revert", _map);
			int result_zk = this.dao.delete("ZMCPProducePlanZK.revert", _map);
			int result_glaz = this.dao.delete("ZMCPProducePlanGLAZ.revert", _map);
			
			//3.0修改专业计划状态，根据上报部门ID
			if(result_zc > 0 || result_zk > 0 || result_glaz > 0) {
				_map.put("fdPlanStatus", "上报");
				_map.put("fdKeyId", fdProfessionalplanId);
				ProfessionalPlan.revert(_map);
			}
		}
		
		out = this.query(inInfo);
		if(message.equals("")) {
			out.setStatus(1);
			out.setMsg("操作" + rowcount + "记录成功");
		}
		else {
			out.setStatus(-1);
			out.setMsg(message);
		}
		return out;  
	}
	
	/**
	 * 判断专项计划或生产计划是否未上报,true表示计划未上报,false表示计划已上报
	 */
	public EiInfo isNotReport(EiInfo inInfo) {
		boolean isNotReport = false;
		if (inInfo.getString("fdProfessionalplanId") != null) {// 专业计划
			isNotReport = ProfessionalPlan.validate(inInfo.getString("fdProfessionalplanId"));
			if (isNotReport == false) {
				inInfo.setStatus(2);
				inInfo.setMsg("当月的专业计划已经上报或作废，不能操作！");
			}
		} else if (inInfo.getString("fdProduceplanId") != null) {// 生产计划
			isNotReport = ProducePlan.validate(inInfo.getString("fdProduceplanId"));
			if (isNotReport == false) {
				inInfo.setStatus(2);
				inInfo.setMsg("当月的生产计划已经上报或作废，不能操作！");
			}
		}
		return inInfo;
	}
}
